<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="标签" prop="tag">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名称" prop="originalFileName">
        <el-input
          v-model="queryParams.originalFileName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.tiff_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上传时间" prop="rangeTime">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:tiff:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:tiff:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:tiff:remove']"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tiffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件名" align="center" prop="originalFileName" />
      <el-table-column label="文件大小" align="center" prop="fileSize" >
        <template slot-scope="scope">
          <span> {{scope.row.fileSize}} MB</span>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="tag" />
      <el-table-column label="是否已上传" align="center" prop="isSeg" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.uploadStatus == '1' " type="success"> 已上传 </el-tag>
          <el-tag v-else-if="scope.row.uploadStatus == '2' " type="warning"> 上传中 </el-tag>
          <el-tag v-else type="info"> 未上传完成 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否已分割" align="center" prop="isSeg" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isSeg == 1" type="success"> 已分割 </el-tag>
          <el-tag v-else-if="scope.row.isSeg == 2" type="warning"> 分割中 </el-tag>
          <el-tag v-else type="info"> 未处理 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status == '1'"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="statusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
<!--      <el-table-column label="分享密码" align="center" prop="sharePassword" width="250">-->
<!--        <template slot-scope="scope">-->
<!--          <span style="margin-right: 10px">{{ scope.row.sharePassword }}</span>-->
<!--          <el-button icon="el-icon-document-copy" type="text" size="small"  v-clipboard:copy="scope.row.sharePassword"></el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:tiff:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.uploadStatus == '1' && scope.row.isSeg == 0"
            size="small"
            type="primary"
            @click="handleSegment(scope.row)"
            v-hasPermi="['business:tiff:edit']"
          >分割</el-button>
          <el-button
            v-if="scope.row.uploadStatus == '1' "
            size="small"
            type="primary"
            @click="handleDownload(scope.row)"
            v-hasPermi="['business:tiff:edit']"
          >下載</el-button>
          <el-button
            v-if="scope.row.isSeg == 1"
            size="small"
            type="warning"
            @click="handlePreview(scope.row)"
            v-hasPermi="['business:tiff:edit']"
          >预览</el-button>
<!--          <el-button-->
<!--            v-if="scope.row.isSeg == 1"-->
<!--            size="small"-->
<!--            type="warning"-->
<!--            @click="handleShare(scope.row)"-->
<!--            v-hasPermi="['business:tiff:edit']"-->
<!--          >分享</el-button>-->
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:tiff:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item v-if="isAdd" label="文件" prop="remark">
          <el-upload
            class="upload-demo"
            :action="uploadFileUrl"
            :on-remove="handleRemove"
            :before-upload="handleBeforeUpload"
            :on-success="handleSuccess"
            :headers="headers"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 tif / tiff 文件，且不超过5GB</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分享对话框 -->
<!--    <el-dialog :title="title" :visible.sync="share_open" width="500px" append-to-body>-->

<!--      <p>分享密码：{{ sharePassword }}</p>-->
<!--      <el-button v-clipboard:copy="sharePassword" type="primary">复制密码</el-button>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click="share_open = false">关 闭</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { listTiff, getTiff, addTiff, updateTiff, delTiff, previewTiff, changeTiffStatus, downloadTiff, createShareUrl, segment } from "@/api/business/tiff"
import { getToken } from "@/utils/auth"
import OpenSeadragon from 'openseadragon'

export default {
  name: "Tiff",
  dicts: ['tiff_status'],
  data() {
    return {
      publicPath: process.env.BASE_URL,
      // 遮罩层
      loading: true,
      // 文件上传设置
      fileList: [],
      fileType: ["tif", "tiff"],
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 大小限制(MB)
      fileSize: 5120,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      //上传文件类型
      uploadFileType: ["tif", "tiff"],
      // 上传文件服务器地址
      uploadFileUrl: "/business/tiff/upload",
      // 岗位表格数据
      tiffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isAdd: false,
      // 是否显示分享弹出层
      share_open: false,
      sharePassword: undefined,
      // 查询参数

      // 日期范围
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        originalFileName: undefined,
        tag: undefined,
        status: undefined
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listTiff(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.tiffList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 预览取消按钮
    preview_cancel() {
      this.preview_open = false
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tag: undefined,
        sharePassword: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加Tiff文件"
      this.fileList = []
      this.isAdd = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.isAdd = false
      const id = row.id || this.ids
      getTiff(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改Tiff文件"
      })
    },
    /** 修改按钮操作 */
    handleSegment(row) {
      this.reset()
      this.isAdd = false
      const id = row.id || this.ids
      segment(id).then(response => {
        this.$modal.msgSuccess(response.msg)
      })
    },
    /** 修改按钮操作 */
    statusChange(row) {

      let status = row.status
      if (status === '1') {
        status = '0'
      } else {
        status = '1'
      }
      const id = row.id
      changeTiffStatus(id, status).then(response => {
        this.$modal.msgSuccess("修改成功")
        this.getList()
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      if (this.form.id != undefined) {
        updateTiff(this.form).then(response => {
          this.$modal.msgSuccess("修改成功")
          this.open = false
          this.getList()
        })
      } else {
        addTiff(this.form).then(response => {
          this.$modal.msgSuccess("新增成功")
          this.open = false
          this.getList()
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delTiff(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 分享按钮操作 */
    handleShare(row) {
      let id = row.id
      createShareUrl(id).then(response => {
        this.sharePassword = response.sharePassword
        this.share_open = true
        this.getList()
      })
    },
    /** 预览按钮操作 */
    handlePreview(row) {
      let id = row.id
      previewTiff(id).then(response => {
        window.open(response.previewUrl, '_blank')
      })
    },
    /** 下载按钮操作 */
    handleDownload(row) {
      const id = row.id
      downloadTiff(id).then(response => {
        let downloadUrl = response.downloadUrl
        window.open(downloadUrl, "_blank")
      })
    },
    /** 处理文件上传 */
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleSuccess(res, file) {
      this.form.fileName = res.fileName
      this.form.newFileName = res.newFileName
      this.form.originalFileName = res.originalFileName
      this.form.objectName = res.objectName
      console.log('handleSuccess -- res', res)
      console.log('handleSuccess -- form', this.form)
    },
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.')
        const fileExt = fileName[fileName.length - 1]
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确，请上传${this.fileType.join("/")}格式文件!`)
          return false
        }
      }
      // 校检文件名是否包含特殊字符
      if (file.name.includes(',')) {
        this.$modal.msgError('文件名不正确，不能包含英文逗号!')
        return false
      }
      // 校检文件大小
      if (this.fileSize) {
        this.form.fileSize = file.size / 1024 / 1024
        const isLt = file.size / 1024 / 1024 < this.fileSize
        if (!isLt) {
          this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
          return false
        }
      }
      return true
    }
  }
}
</script>

<style scoped>
.viewer {
  width: 100%;
  height: 600px;
  border: 1px solid #ddd;
}
</style>
